// 封装分类相关的代码
import { ref, onMounted } from "vue";
import { getCategoryAPI } from "@/apis/category";
import { onBeforeRouteUpdate } from "vue-router";
import { useRoute } from "vue-router";
export function useCategory() {
    const categoryData = ref({})
    const route = useRoute()
    const getCategory = async (id = route.params.id) => {
        const res = await getCategoryAPI(id)
        categoryData.value = res.result
    }
    onMounted(() => {
        getCategory()
    })
    //路由守卫
    //接受参数：to 即将要进入的目标，from
    onBeforeRouteUpdate((to) => {
        //路由变化了
        getCategory(to.params.id)
    })
    
    return { categoryData };
}
